'''class musicplayer(object):
    instance = None
    def __new__(cls,*args,**kwargs):
        #1创建对象时，new方法会自动调用

        if(cls.instance==None):
            cls.instance=super().__new__(cls)
        #3返回
        return cls.instance
    def __init__(self,music_name):
        print("播放器初始化%s"%music_name)
        self.music_name=music_name
'''
class musicplayer(object):
    def __new__(cls,*args,**kwargs):
        print("分配空间")
        instance=object.__new__(cls)
        return instance
    def __init__(self):
        print("播放")

if __name__=='__main__':
    player1=musicplayer()
    player2=musicplayer()
    print(id(player1))
    print(id(player2))

print("-"*100)
# def use_expect():
#     '''
#     练习简单的捕获异常
#     :return:
#     '''
#     while True:
#       try:
#           num=int(input("请输入一个数字："))
#           print(num)
#           break
#       except:
#           print('请输入数字')
'''提示用户输入一个整数，使用8除以用于输入的整数并且输出'''
# def use_except_more_type():
#     '''
#     使用不同的异常类型
#     :return:
#     '''
#     try:
#         num=int(input("请输入整数："))
#         result=8/num
#         print(result)
#     except ValueError:
#         print('请输入正确的整数')
#     except ZeroDivisionError:
#         print('不可与输入0')
#     except Exception as result:
#         print(result)
#     else:
#         print('正常执行')

# if __name__=='__main__':
#     # use_except_more_type()
# print("-"*100)
# use_expect()

def print_exception_pos(e):
    '''
    打印异常位置用函数封装起来
    :return:
    '''
    print(e)
    print(e.__traceback__.tb_frame.f_globals["__file__"])
    print(e.__traceback__.tb_lineno)
try:
    a=1/0
except Exception as e:
    print_exception_pos(e)

def demo1():
    num=int(input("请输入一个整数"))
    print(f'demo1 {num}')
    return num
def demo2():
    num=demo1()
    print(f'demo2 {num}')
    return num




try:
    print(demo2())
except Exception as e:
    print(e)
#自定义异常
def input_password():
    '''
    自定义异常并提出
    :return:
    '''
    password=input('请输入密码')
    # 断言异常的使用，如果密码位数大于8则打印此处发生了什么什么异常
    assert len(password) >= 8,'此处发生了什么什么异常'
    # 否则返回密码
    return password

try:
    password=input_password()
    print(password)
except Exception as e:
    print(e)

